<?php $this->load->view('intro'); ?>
<body id="food_option_category">
<?php $this->load->view('admin/navigation'); ?>
<div id="edit_section">
<div id="message_block" style="width:600px;""><?php echo $message ?></div>
<form id="main_form" action="index.php/admin/food_option_category/add" method="post">
  <input type="hidden" value="" id="target_id" name="target_id" />
  <label for="category_en_input">Category: </label>
  <input name="en_name" maxlength="64" type="text" id="category_en_input" style="width:150px;" value="" />
  <label for="category_ch_input">Category(CH): </label>
  <input name="ch_name" maxlength="64" type="text" style="width:150px;" id="category_ch_input" value="" />
  <label for="sequence_input">Sequence: </label>
  <input name="sequence" maxlength="3" type="text" style="width:50px;" id="sequence_input" value="" />
    <select name="scope" id="scope">
    <option value="quick">quick</option>
    <option value="all">all</option>
  </select>
    <br /><br />
  <input id="add_form_button" style="display:inline-block; width:140px; height:50px;" type="submit" value="add">
  <input style="display:inline-block; width:140px; height:50px;" type="submit" value="update">
</form>
<br />
<hr style="border:1px solid #CCC;" />
<table style="border-spacing: 15px;">
<tr><th>en_name</th><th>ch_name</th><th>sequence</th><th>scope</th><th>action</th></tr>
<?php $sequence_opt = ""; ?>
<?php foreach($option_category as $option) { ?>
<?php $sequence_opt .= "<option value=\"" . $option['id'] . "\">" . $option['sequence'] . "</option>"; ?>
<tr><td><?php echo $option['en_name']; ?></td><td><?php echo $option['ch_name']; ?></td>
    <td><?php echo $option['sequence']; ?></td><td><?Php echo $option['scope']; ?></td>
<td><span onclick="update(<?php echo $option['id']; ?>);">Edit</span>&nbsp;&nbsp
    <span onclick="del(<?php echo $option['id']; ?>);">Delete</span></td></tr>
<?php } ?>
</table>

<form action="index.php/admin/food_option_category/swap" method="post">
  <select name="swap_from" id="swap_from"><?php echo $sequence_opt; ?></select>
  <select name="swap_to" id="swap_to"><?php echo $sequence_opt; ?></select>
  <input style="display:block; width:140px; height:50px;" type="submit" value="swap category">
</form>
<!-- delete helper -->
<form id="delete_form" action="index.php/admin/food_option_category/delete" method="post">
    <input type="hidden" id="delete_id" name="delete_id" val="" />
</form>

<script type="text/javascript">
function update($id)
{
      $data = {target_id:  $id};
      $.ajax({
        type: "POST",
        url: "index.php/admin/food_option_category/read",
        dataType: "json",
        data: $data,
        success: function(msg){
          $en_name = msg.en_name;
          $ch_name = msg.ch_name;
          $sequence = msg.sequence;
          $scope = msg.scope;
          $("#target_id").val(msg.id);
          $("#category_en_input").val($en_name);
          $("#category_ch_input").val($ch_name);
          $("#sequence_input").val($sequence);
          $("#scope").val($scope);
          $("#main_form").attr("action", "index.php/admin/food_option_category/update");
          $("#add_form_button").attr("disabled", "disabled");
        }
      });
}

function del($id)
{
    $("#delete_id").val($id);
    $("#delete_form").submit();
}
</script>
<?php $this->load->view('outro'); ?>